#!/bin/bash

# 打包部署脚本
# 作者：自动生成
# 用途：打包Java应用并部署到指定目录

set -e  # 遇到错误立即退出

echo "=== 开始打包部署 xiaozhi-esp32-api ==="

# 定义变量
PROJECT_DIR="/root/git/xiaozhi-esp32-server/main/manager-api"
BUILD_DIR="/root/git/xiaozhi-esp32-server/build"
JAR_NAME="xiaozhi-esp32-api.jar"
APP_NAME="xiaozhi-esp32-api"

# 进入项目目录
cd "$PROJECT_DIR"

echo "1. 清理并打包项目..."
mvn clean package -DskipTests

# 检查打包是否成功
if [ ! -f "target/$JAR_NAME" ]; then
    echo "错误：打包失败，未找到 target/$JAR_NAME"
    exit 1
fi

echo "2. 创建部署目录..."
mkdir -p "$BUILD_DIR"

echo "3. 复制JAR文件到部署目录..."
cp "target/$JAR_NAME" "$BUILD_DIR/"

echo "4. 复制配置文件到部署目录..."
# 复制配置文件
cp "src/main/resources/application.yml" "$BUILD_DIR/" 2>/dev/null || echo "警告：application.yml 不存在"
cp "src/main/resources/application-dev.yml" "$BUILD_DIR/" 2>/dev/null || echo "警告：application-dev.yml 不存在"
cp "src/main/resources/logback-spring.xml" "$BUILD_DIR/" 2>/dev/null || echo "警告：logback-spring.xml 不存在"

echo "5. 创建启动脚本..."
cat > "$BUILD_DIR/start.sh" << 'EOF'
#!/bin/bash

# xiaozhi-esp32-api 启动脚本
# 使用 application-dev.yml 配置和 logback-spring.xml 日志配置

APP_NAME="xiaozhi-esp32-api"
JAR_FILE="xiaozhi-esp32-api.jar"
PID_FILE="${APP_NAME}.pid"
LOG_FILE="logs/${APP_NAME}.log"

# 创建日志目录
mkdir -p logs

# 检查JAR文件是否存在
if [ ! -f "$JAR_FILE" ]; then
    echo "错误：未找到 $JAR_FILE"
    exit 1
fi

# 检查是否已经在运行
if [ -f "$PID_FILE" ]; then
    PID=$(cat $PID_FILE)
    if ps -p $PID > /dev/null 2>&1; then
        echo "$APP_NAME 已经在运行 (PID: $PID)"
        exit 1
    else
        echo "删除过期的PID文件: $PID_FILE"
        rm -f "$PID_FILE"
    fi
fi

echo "启动 $APP_NAME..."
echo "配置文件: application-dev.yml"
echo "日志配置: logback-spring.xml"
echo "日志文件: $LOG_FILE"

# 启动应用，指定配置文件和日志配置
nohup java -jar $JAR_FILE \
    --spring.profiles.active=dev \
    --spring.config.location=classpath:/application.yml,./application-dev.yml \
    --logging.config=./logback-spring.xml \
    > "$LOG_FILE" 2>&1 &

# 保存PID
echo $! > "$PID_FILE"

echo "$APP_NAME 启动成功!"
echo "PID: $(cat $PID_FILE)"
echo "查看日志: tail -f $LOG_FILE"
EOF

echo "6. 创建停止脚本..."
cat > "$BUILD_DIR/stop.sh" << 'EOF'
#!/bin/bash

# xiaozhi-esp32-api 停止脚本

APP_NAME="xiaozhi-esp32-api"
PID_FILE="${APP_NAME}.pid"

if [ ! -f "$PID_FILE" ]; then
    echo "$APP_NAME 未在运行"
    exit 1
fi

PID=$(cat $PID_FILE)

if ps -p $PID > /dev/null 2>&1; then
    echo "停止 $APP_NAME (PID: $PID)..."
    kill $PID
    
    # 等待进程结束
    sleep 3
    
    if ps -p $PID > /dev/null 2>&1; then
        echo "强制停止 $APP_NAME..."
        kill -9 $PID
    fi
    
    rm -f "$PID_FILE"
    echo "$APP_NAME 已停止"
else
    echo "$APP_NAME 进程不存在，清理PID文件"
    rm -f "$PID_FILE"
fi
EOF

echo "7. 创建重启脚本..."
cat > "$BUILD_DIR/restart.sh" << 'EOF'
#!/bin/bash

# xiaozhi-esp32-api 重启脚本

echo "重启 xiaozhi-esp32-api..."
./stop.sh
sleep 2
./start.sh
EOF

echo "8. 创建状态查看脚本..."
cat > "$BUILD_DIR/status.sh" << 'EOF'
#!/bin/bash

# xiaozhi-esp32-api 状态查看脚本

APP_NAME="xiaozhi-esp32-api"
PID_FILE="${APP_NAME}.pid"

if [ -f "$PID_FILE" ]; then
    PID=$(cat $PID_FILE)
    if ps -p $PID > /dev/null 2>&1; then
        echo "$APP_NAME 正在运行 (PID: $PID)"
        echo "内存使用情况:"
        ps -p $PID -o pid,ppid,cmd,%mem,%cpu --no-headers
    else
        echo "$APP_NAME 未运行（PID文件存在但进程不存在）"
    fi
else
    echo "$APP_NAME 未运行"
fi
EOF

echo "9. 设置脚本执行权限..."
chmod +x "$BUILD_DIR/start.sh"
chmod +x "$BUILD_DIR/stop.sh"
chmod +x "$BUILD_DIR/restart.sh"
chmod +x "$BUILD_DIR/status.sh"

echo ""
echo "=== 部署完成 ==="
echo "部署目录: $BUILD_DIR"
echo "生成的文件:"
ls -la "$BUILD_DIR"

echo ""
echo "=== 使用说明 ==="
echo "进入部署目录: cd $BUILD_DIR"
echo "启动应用:     ./start.sh"
echo "停止应用:     ./stop.sh"
echo "重启应用:     ./restart.sh"
echo "查看状态:     ./status.sh"
echo "查看日志:     tail -f logs/xiaozhi-esp32-api.log"

echo ""
echo "=== 部署成功！===" 